package com.hfjyz.judge.judgecall.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.judge.judge.bean.JudgeBean;
import com.hfjyz.judge.judgecall.bean.JudgeCallBean;
import com.hfjyz.judge.judgecall.bean.JudgeCallSearchBean;
import com.hfjyz.judge.judgecall.bean.JudgeCallSearchCondition;
import com.hfjyz.judge.judgecall.service.JudgeCallService;

public class JudgeCallAction extends GridBaseAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -329879627858090900L;
    private static final Log SYS_LOG = LogFactory.getLog(BaseConstant.SYS_LOG);
    private JudgeCallService service = null;
    private JudgeCallBean judgeCall = null;
    private String selectIds = null;
    private Integer judgeId = null;
    protected JudgeCallSearchBean judgeCallSearch = null;

    public JudgeCallService getService() {
        return service;
    }

    public void setService(JudgeCallService service) {
        this.service = service;
    }

    public JudgeCallBean getJudgeCall() {
        return judgeCall;
    }

    public void setJudgeCall(JudgeCallBean judgeCall) {
        this.judgeCall = judgeCall;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectIds) {
        this.selectIds = selectIds;
    }

    public Integer getJudgeId() {
        return judgeId;
    }

    public void setJudgeId(Integer judgeId) {
        this.judgeId = judgeId;
    }

    public JudgeCallSearchBean getJudgeCallSearch() {
        return judgeCallSearch;
    }

    public void setJudgeCallSearch(JudgeCallSearchBean judgeCallSearch) {
        this.judgeCallSearch = judgeCallSearch;
    }

    @Override
    protected List<JudgeCallBean> getBeanList() throws BaseException {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute(BaseConstant.SESSION_CONDITION_KEY, judgeCallSearch);

        return service.findJudgeCall(getSearchCondition(), judgeCallSearch);
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getJudgeCallCount(judgeCallSearch);
    }

    public String save() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            service.saveJudgeCall(judgeCall);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            if (selectIds == null) {
                SYS_LOG.error("未选择待删除的评委调用！");
                throw new BaseException("未选择待删除的评委调用！");
            }
            String[] idArray = selectIds.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteJudgeCall(idList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String updateInit() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            Integer id = Integer.valueOf(selectIds);
            judgeCall = service.updateInitJudgeCall(id);
            List<JudgeBean> judgeList = service.findAllJudge();

            map.put("judgeCall", judgeCall);
            map.put("judgeList", judgeList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String listJudge() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            List<JudgeBean> judgeList = service.findAllJudge();
            map.put("judgeList", judgeList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public SearchCondition getSearchCondition() {
        SearchCondition cond = new JudgeCallSearchCondition();
        cond.setPageNum(pageNum);
        cond.setRowCountPerPage(rowCountPerPage);
        cond.setSortColumn(sortColumn);
        cond.setSortDirection(sortDirection);

        return cond;
    }
}
